package dev.html;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

public class MyParserCallback extends ParserCallback{
	
	private Map<String, List<String>> tags = new HashMap<String, List<String>>();
	private Map<String, Deque<Integer>> stacks = new HashMap<String, Deque<Integer>>();

	private String htmlPage = "";
	
	
	public MyParserCallback(String htmlPage){
		super();
		this.htmlPage = htmlPage;
	}
	
	public List<String> getTag(String name){
		List<String> tag = tags.get(name);
		if(tag == null){
			tag = new LinkedList<String>();
			tags.put(name, tag);
		}
		return tag;
	}
	
	public List<String> getTagAndAttribute(String tagName, String attrName, String attrValue){
		List<String> tags = new LinkedList<String>();
		Iterator<String> it = getTag(tagName).iterator();
		while(it.hasNext()){
			String tag = it.next();
			String tagHead = tag.substring(tag.indexOf("<"), tag.indexOf(">"));
			if(tagHead.matches(".+\\s+" + attrName + "\\s*\\=\\s*['\"/]*" + attrValue + "['\"/]*\\s*.+")){
				tags.add(tag);
			}
		}
		return tags;
	}
	
	private Deque<Integer> getStack(String name){
		Deque<Integer> stack = stacks.get(name);
		if(stack == null){
			stack = new ArrayDeque<Integer>();
			stacks.put(name, stack);
		}
		return stack;
	}
	
	
	public void handleComment(char[] data, int pos){
		
	}
	public void handleError(String errorMsg, int pos){
		
	}
	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos){
		
	}
	
	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos){
		getStack(t.toString()).push(pos);
	}
	
	public void handleEndTag(HTML.Tag t, int pos){
		pos = pos + t.toString().length() + 3;
		
		String tag = htmlPage.substring(getStack(t.toString()).pop(), pos);
		getTag(t.toString()).add(tag);
		
		/*
		if(t == HTML.Tag.DIV){
			String tag = htmlPage.substring(stackDIV.pop(), pos);
			divs.add(tag);
			if(tag.indexOf("class=\"box-user\"")>=0 && counter>0){
				processTag(tag);
				counter--;
			}
		}*/
	}
	private void processTag(String tag) {
		String clsName = "<span class=\"username\">";
		String clsCtry = "<span class=\"country-travel\">";
		String clsTime =  "<div class=\"time\">";
		String spanEnd = "</span>";
		String divEnd = "</div>";
		
		int namei = tag.indexOf(clsName)+clsName.length();
		int namee = tag.indexOf(spanEnd);
		int timei = tag.indexOf(clsTime)+clsTime.length();
		int timee = tag.indexOf(divEnd);
		System.out.println("" + HTMLParse.counter++ + "\t" + tag.substring(namei, namee) 
				+ "\t" + tag.substring(timei, timee));
	}


	public void handleText(char[] data,int pos){
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String tagHead = "<div id2 = you/ >";
		String attrName = "id2";
		String attrValue = "you";
		
		System.out.println(
				tagHead.matches(".+\\s+" + attrName + "\\s*\\=\\s*['\"/]*" + attrValue + "['\"/]*\\s*.+")
				);

	}

}
